﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using memo;
using System.Collections.Generic;
using System.IO;

namespace memoTest
{
    /// <summary>
    /// Tests the functionality of the DataAccessor.
    /// </summary>
    [TestClass]
    public class DataAccessorTest
    {
        /// <summary>
        /// The tested DataAccessor, and a Topic list, that is going to be saved and returned.
        /// </summary>
        private DataAccessor myDataAccessor;
        private List<Topic> myTopics;

        /// <summary>
        /// Sets up the fields.
        /// </summary>
        public DataAccessorTest()
        {
            myDataAccessor = new DataAccessor();
            myTopics = new List<Topic>();
            for (int i = 0; i <= 5; i++)
            {
                myTopics.Add(new Topic("Title" + i, "Short description" + i));
            }
        }
        /// <summary>
        /// Deletes the file in the myDataAccessor's path and tries to read the file.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(MemoDataAccessorException))]
        public void DeleteDataAndTryException()
        {
            File.Delete(myDataAccessor.SavedFilePath);
            myTopics = myDataAccessor.Load();
            Assert.Fail("The FileNotFoundException was not thrown, probably the file couldn't be deleted.");

        }

        /// <summary>
        /// Generates some topics and tries to save them.
        /// </summary>
        [TestMethod]
        public void SaveData()
        {
            myDataAccessor.Save(myTopics);
        }

        /// <summary>
        /// Uses DataAccessor.Save() to save some topics and then loads the data and compares the topics with the loaded ones.
        /// </summary>
        [TestMethod]
        public void LoadData()
        {
            myDataAccessor.Save(myTopics);
            List<Topic> LoadedTopics = myDataAccessor.Load();
            CollectionAssert.AreEqual(LoadedTopics, myTopics);
        }

    }
}